package com.sdy.healthcode.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sdy.common.model.BizException;
import com.sdy.common.model.Response;
import com.sdy.common.utils.Assert;
import com.sdy.healthcode.biz.dto.HealthCodeAnalyze;
import com.sdy.healthcode.biz.dto.ReportPeopleOrderDTO;
import com.sdy.healthcode.biz.model.ReportPeopleOrder;
import com.sdy.healthcode.biz.service.HealthCodeService;
import com.sdy.healthcode.biz.service.ReportPeopleOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Glm
 */
@Slf4j
@RestController
@Api(tags = "健康码情况分析+预约情况")
@RequestMapping("/HealthCodeAnalyzeController")
public class HealthCodeAnalyzeController {
    @Autowired
    private HealthCodeService healthCodeService;
    @Autowired
    private ReportPeopleOrderService reportPeopleOrderService;

    /**
     * 健康码比例分析
     *
     * @return
     */
    @GetMapping("/codeAnalyze")
    public Response codeAnalyze() throws BizException {
        List<Map<String, Object>> hospitalNumMap = healthCodeService.getHospitalNum();
        List<Map<String, Object>> totalNumMap = healthCodeService.getTotalNum();
        Long hospitalGreen =0L;
        Long hospitalYellow =0L;
        Long hospitalRed=0L;
        for (Map<String, Object> hm:hospitalNumMap) {
            if (hm.get("hcode").equals("G")){
                hospitalGreen = (Long) hm.get("num");
            }else if (hm.get("hcode").equals("Y")){
               hospitalYellow = (Long) hm.get("num");
            }else if (hm.get("hcode").equals("R")){
                hospitalRed = (Long) hm.get("num");
            }
        }
        Long hospitalTotal = hospitalGreen + hospitalYellow + hospitalRed;
        // 拿到本院红黄绿码比例
        String hospitalGreenScale = getScale(hospitalGreen, hospitalTotal);
        String hospitalYellowScale = getScale(hospitalYellow, hospitalTotal);
        String hospitalRedScale = getScale(hospitalRed, hospitalTotal);
        Long totalGreen =0L;
        Long totalYellow =0L;
        Long totalRed=0L;
        for (Map<String, Object> hm: totalNumMap) {
            if (hm.get("hcode").equals("G")){
                totalGreen = (Long) hm.get("num");
            }else if (hm.get("hcode").equals("Y")){
                totalYellow = (Long) hm.get("num");
            }else if (hm.get("hcode").equals("R")){
                totalRed = (Long) hm.get("num");
            }
        }
        Long total = totalGreen + totalYellow + totalRed;
        // 拿到所有红黄绿码比例
        String totalGreenScale = getScale(totalGreen, total);
        String totalYellowScale = getScale(totalYellow, total);
        String totalRedScale = getScale(totalRed, total);

        List<HealthCodeAnalyze> healthCodeAnalyzeList = new ArrayList<>();
        HealthCodeAnalyze redCodeAnalyze = new HealthCodeAnalyze();
        HealthCodeAnalyze yellowCodeAnalyze = new HealthCodeAnalyze();
        HealthCodeAnalyze greenCodeAnalyze = new HealthCodeAnalyze();
        redCodeAnalyze.setHcode("红码")
                .setHospitalNum(hospitalRed)
                .setHospitalScale(hospitalRedScale)
                .setTotalNum(totalRed)
                .setTotalScale(totalRedScale);
        healthCodeAnalyzeList.add(redCodeAnalyze);
        yellowCodeAnalyze.setHcode("黄码")
                .setHospitalNum(hospitalYellow)
                .setHospitalScale(hospitalYellowScale)
                .setTotalNum(totalYellow)
                .setTotalScale(totalYellowScale);
        healthCodeAnalyzeList.add(yellowCodeAnalyze);
        greenCodeAnalyze.setHcode("绿码")
                .setHospitalNum(hospitalGreen)
                .setHospitalScale(hospitalGreenScale)
                .setTotalNum(totalGreen)
                .setTotalScale(totalGreenScale);
        healthCodeAnalyzeList.add(greenCodeAnalyze);
        return Response.success(healthCodeAnalyzeList);
    }

    /**
     * 获取预约信息
     *
     * @return
     */
    @GetMapping("/getReportPeopleOrder")
    public Response getReportPeopleOrder() {
        SimpleDateFormat dkf = new SimpleDateFormat("yyyyMMdd");
        String today = dkf.format(new Date());
        List<ReportPeopleOrder> reportPeopleOrderList = reportPeopleOrderService.list(new LambdaQueryWrapper<ReportPeopleOrder>()
                .eq(ReportPeopleOrder::getRecordDate,today));
        List<ReportPeopleOrderDTO> reportPeopleOrderDTOList = new ArrayList<>();
        if (!reportPeopleOrderList.isEmpty()) {
            for (ReportPeopleOrder peopleOrder : reportPeopleOrderList) {
                ReportPeopleOrderDTO reportPeopleOrderDTO = new ReportPeopleOrderDTO();
                Date recordTime = peopleOrder.getRecordTime();
                SimpleDateFormat dkf1 = new SimpleDateFormat("yyyy/MM/dd HH:mm");
                String datetime = dkf1.format(recordTime);
                reportPeopleOrderDTO.setId(peopleOrder.getId())
                        .setOrderNum(peopleOrder.getOrderNum())
                        .setArriveNum(peopleOrder.getArriveNum())
                        .setWaitNum(peopleOrder.getWaitNum())
                        .setRecordDate(datetime);
                reportPeopleOrderDTOList.add(reportPeopleOrderDTO);
            }
        }
        return Response.success(reportPeopleOrderDTOList);
    }

    private String getScale(Long size, Long size2) throws BizException {
        String orderRate =null;
        if (size2 == null || size2 == 0){
           return orderRate="100.00%";
        }
        DecimalFormat df = new DecimalFormat();
        df.setMaximumFractionDigits(2);
        df.setMinimumFractionDigits(2);
        orderRate = df.format(size * 100.00 / size2) + "%";
        return orderRate;
    }
}
